package org.nox.helloseries.core.connector.tracker.impl;

import org.jdom2.Element;
import org.nox.helloseries.core.connector.tracker.AbstractRSSTorrentSearch;
import org.nox.helloseries.domain.NomTracker;
import org.nox.helloseries.domain.Serie;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExtraTorrentSearch extends AbstractRSSTorrentSearch
{

    private static Logger LOG = LoggerFactory.getLogger( ExtraTorrentSearch.class );

    private final String BASE_URL = "http://extratorrent.cc/rss.xml?type=search&cid=8&search=";

    protected String getTitre( Element e )
    {
        String titre = e.getChildText( "title" );
        return titre;
    }

    protected String getUrl( Element e )
    {
        String url = e.getChild( "enclosure" ).getAttributeValue( "url" ).replace( "&amp;", "&" );
        return url;
    }

    @Override
    protected String getUrlRecherche( Serie serie )
    {
        String url = BASE_URL;

        return url + ( serie.toString() + serie.getQualite().getValue() ).trim().replaceAll( " ", "+" );
    }

    @Override
    protected double getTailleFichier( Element e )
    {
        double size = Double.parseDouble( e.getChild( "enclosure" ).getAttributeValue( "length" ) ) / 1000000;
        return size;
    }

    @Override
    protected int getSeeds( Element e )
    {
        int seeds = 0;
        try
        {
            seeds = Integer.parseInt( e.getChild( "seeders" ).getValue() );
        }
        catch ( Exception ex )
        {
            LOG.debug( "Impossible de trouver les Seeds : " + ex.getMessage() );
        }
        return seeds;
    }

    @Override
    protected NomTracker getNomTracker()
    {
        return NomTracker.EXTRATORRENT;
    }

}
